import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './index.less';
import {Button,WingBlank,List,InputItem,Toast} from 'antd-mobile';

import { FormGroup,FormControl } from 'react-bootstrap';
import Block from 'fs-flex'
import Header from '../../../components/Header/index'
import WindowService from '../../../services/windowService'
const Item=List.Item
class PaihaoPage extends React.Component{
    state={
        hasError:false
    }

  async login(){
      //const {form,dispatch}  =this.props
    const {account,password} = this.props.form.getFieldsValue()
   
    if(!account){
      Toast.fail('请输入账号',1)
      return
    }
    if(!password){
      Toast.fail('请输入密码',1)
      return
    }
      const {code,msg,data} = await WindowService.login({account:account,password:password})
      console.log(msg)
      if(code==="0000"){
          let storage = window.localStorage
          storage.setItem('account',data.account)
          storage.setItem('token',data.token)
          if(data.account==='admin'){
              //this.props.history.push('/manage/setting')
              window.location.href='#/manage/setting'
          }else{
              //this.props.history.push('/business')
              window.location.href='#/business'
          }
      }else{
          Toast.fail(msg,3)
      }
  }

  render(){
    const { getFieldProps,getFieldError } = this.props.form;
    return (
      <Block vf>
        <Block f='1' vf>
          <WingBlank>
            <Block mt={30} style={{backgroundColor:'#fff'}}>
              <List>
                <InputItem {...getFieldProps('account')}
                  type="text"
                  placeholder="请输入账号"
                  error={this.state.hasError}
                  onErrorClick={()=>{
                    if (this.state.hasError) {
                      Toast.info('请输入正确的账号');
                    }
                  }}
                >账号</InputItem>
              </List>
              <List>
                <InputItem {...getFieldProps('password')}
                  type="password"
                  placeholder="密码"
                >密码</InputItem>
              </List>

              <Block wf mt={15} style={{paddingBottom:'15px'}}>
                <Block f='3'></Block>
                <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={()=>this.login()}>登录</Button></Block>
                <Block f='3'></Block>
              </Block>
            </Block>
          </WingBlank>
        </Block>
      </Block>
    );
  }
  
}

PaihaoPage.propTypes = {
};

export default createForm()(connect()(PaihaoPage));
